Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Java String

  • String refers to a group of characters. String, however, refers to an object in Java that represents a series of characters. Java.lang is used. A string object is made using the String class.
    • In Java, a string is internally represented as a character array. The main distinction between this and other programming languages like C is that here, String is not NULL terminated.
    • In Java, a string is immutable, which means that once an object of that type has been created, it cannot be changed. Consider using Java's StringBuffer or StringBuilder classes if you want a mutable String.
Table Of Content

  • String in Java
  • String Literal
  • String by new keyword
  • String literals vs String with new Keyword
  • String Buffer in Java
  • String Builder in Java
  • StringTokenizer in Java






  • In Java, the "+" operator is overloaded for String and is typically used to join two Strings together. Operator overloading is a special feature that is only available for the String class and is not supported by Java. Additionally, the "+" operator is implemented internally using the append() methods of the StringBuilder and StringBuffer objects.
  • Two Strings are considered equal if they contain exactly the same character in the same order and case. The String class overrides the equals() and hashcode() methods. Consider using the equalsIgnoreCase() method if you want to ignore case when comparing two strings.
  • Double quotes are used to enclose strings, such as "abcd," and these representations are known as string literals. I'll explain what a string pool is in the next point.
  • In Java, you can create a String from a StringBuffer, a StringBuilder, a byte array, a char array, or another string. Each of these has a constructor provided by the Java String class.






Ways of Creating a String

  • By String Literal
  • Using new Keyword

String Literal


Syntax to Declare String Literal
<String Type> <string variable name> = "<sequence of string>"; 

e.g

String exam= "DockerTpoint";

The "string constant pool" is checked first by the JVM whenever a string literal is created. A reference to the pooled instance of the string is returned if it already exists. If the string is missing from the pool, a new instance of the string is created and added to the pool.


String Litreals



String exam= "DockerTpoint";
String exam1="DockerTpoint"; //No new instance is created.

Java can use the String Literal to save memory (because no new objects are created if it exists already in the string constant pool).


The "string constant pool" is a specific region of memory where string objects are store.


Example of String Literal in java


Example of String Literal in java
 // Java example of String Literal
public class Main {	
	public static void main(String args[]){  		
		String exam= "DockerTpoint";
		String exam1="DockerTpoint";
		System.out.println(exam);
		System.out.println(exam1);
   } 
}
There will only be one object created in the example above. First off, the JVM will create a new object because it cannot find an existing string object in the string constant pool that has the value "DockerTpoint". The string with the value "DockerTpoint" will then be located in the pool; at this point, no new objects will be created; instead, a reference to the original instance will be returned.



Output:

DockerTpoint 
DockerTpoint 


String by new keyword

With a new operator, the string can be allocated dynamically and declared. When a String is dynamically allocated, a new memory location in the heap is given to it. This string won't be included in the pool of String constants.


String str = new String("Examdeva");

You must "intern" this string if you want to keep it in the constant pool.


String interned = str.intern(); 
// the string will be added to the pool of string constants.

Example of String with new Keyword


Example of String with new keyword in java
public class Main {	
	public static void main(String args[]){  		
        String str = "DockerTpoint"; //String Declared without using new operator
        System.out.println("str = " + str);   
        String str1 = new String("DockerTpoint");  //String Declared using new operator
        System.out.println("str1 = " + str1);
   } 
}



Output:

str = DockerTpoint 
str1 = DockerTpoint 

Java String vs new String



Example of String literals vs String with new Keyword


Example of String literals vs String with new Keyword in java
public class Main {	
    public static void main(String args[]){  		 
    String exam1 = "DockerTpoint"; //string using string literal
    String exam2 = "DockerTpoint";
    String exam3 = new String("DockerTpoint"); // strings using new keyword
    String exam4 = new String("DockerTpoint");
    if(exam1 == exam2){
      System.out.println("String exam1 and exam2 are equal");
    }
    else{
      System.out.println("String exam1 and exam2 are not equal");
    }
    if(exam3 == exam4){
      System.out.println("String exam3 and exam4 are equal");
    }
    else{
      System.out.println("String exam3 and exam4 are not equal");
    }
  } 
}



Output:

String exam1 and exam2 are equal 
String exam3 and exam4 are not equal 


StringBuffer in Java


A peer class of String called StringBuffer offers many of the same features as strings. While StringBuffer represents expandable and writable character sequences, the string represents immutable, fixed-length character sequences.


Syntax: Declaration of StringBuffer
StringBuffer str = new StringBuffer("DockerTpoint");

Java StringBuffer Next »

StringBuilder in Java


In Java, a mutable string of characters is represented by the StringBuilder class. The StringBuilder class offers a substitute for the String Class in Java because it generates mutable sequences of characters as opposed to the String Class's immutable ones.


Syntax: Declaration of StringBuilder
StringBuilder sB = new StringBuilder();
sB.append("DockerTpoint");

Java StringBuilder Next »

StringTokenizer in Java


A string can be divided into tokens using Java's StringTokenizer class. Internally, a StringTokenizer object keeps track of where it is in the string that needs to be tokenized. Some operations move this position ahead of the currently processed characters. By extracting a substring from the string that was used to create the StringTokenizer object, a token is returned.





StringTokenizer
String Tokenizer in Java Next »





Why string objects are immutable in java?

Java supports immutable string objects.because Java makes use of the string literal concept. Let's say there are five reference variables, all of which refer to the same thing, "Sachin." All reference variables will be impacted if one reference variable alters the value of the object. Because of this, string objects in Java are immutable.



Java String Class Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java String
Java String Class
Java Immutable String
Java Immutable Class
Java String Buffer
Java String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java

Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.